package cn.smbms.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.smbms.bean.model.FunctionAuthority;
import cn.smbms.bean.vo.FunctionAuthorityVo;
import cn.smbms.biz.IFunctionAuthorityBiz;

@Controller
@RequestMapping("/functionAuthority")
public class FunctionAuthorityController {
	@Resource(name = "functionAuthorityBiz")
	private IFunctionAuthorityBiz biz;
	List<FunctionAuthority> list = null;
	
	@RequestMapping("/get")
	public String get(HttpServletRequest request,FunctionAuthorityVo functionAuthorityVo,Model model) throws UnsupportedEncodingException{	//获取信息
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		list = biz.getFunctionAuthorityByName(name);
		model.addAttribute("list",list);	
		model.addAttribute("functionAuthorityVo",functionAuthorityVo);
		return "functionAuthority/index";
	}

	@RequestMapping("/delete")
	public String delete(@RequestParam(value="name")String name,HttpServletRequest request,Model model) throws UnsupportedEncodingException{	//删除
		request.setCharacterEncoding("utf-8");
		int result = biz.delete(name);
		if(result > 0) {
			model.addAttribute("msg", "删除成功！");
		}else{
			model.addAttribute("msg", "删除失败！");
		}
		return "redirect:/functionAuthority/get";
	}
	
	@RequestMapping("/modifyPage")
	public String modifyPage(@RequestParam(value="name")String name,Model model){	//进入修改页面
		FunctionAuthority entity = biz.findFunctionAuthorityByName(name);
		model.addAttribute("entity",entity);
		return "functionAuthority/modify";
	}
	
	@RequestMapping("/update")
	public String update(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException{	//修改
		request.setCharacterEncoding("utf-8");
		FunctionAuthority entity = biz.findFunctionAuthorityByName(request.getParameter("name"));
		String keyValue = request.getParameter("keyValue");//权限检查值
		int isgroup = Integer.parseInt(request.getParameter("isgroup"));//是否是分组
		//int parentId = Integer.parseInt(request.getParameter("parentId"));//父节点Id
		//int deleteFlag = Integer.parseInt(request.getParameter("deleteFlag"));//删除状态
		entity.setKeyValue(keyValue);
		entity.setIsgroup(isgroup);
		//entity.setParentId(parentId);
		//entity.setDeleteFlag(deleteFlag);
		if(biz.update(entity) > 0){
			model.addAttribute("msg", "修改成功！");
			return "redirect:/functionAuthority/get";
		}else{
			model.addAttribute("msg","修改失败！");
			return "functionAuthority/modify";
		}
	}
	
	@RequestMapping("/addPage")
	public String addPage(){	//进入添加页面
		return "functionAuthority/add";
	}
	
	@RequestMapping("/add")
	public String add(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException{	//添加
		request.setCharacterEncoding("utf-8");
		FunctionAuthority entity = new FunctionAuthority();
		String name = request.getParameter("name");//名称
		String keyValue = request.getParameter("keyValue");//权限检查值
		int isgroup = Integer.parseInt(request.getParameter("isgroup"));//是否是分组
		//int parentId = Integer.parseInt(request.getParameter("parentId"));//父节点Id
		//int deleteFlag = Integer.parseInt(request.getParameter("deleteFlag"));//删除状态
		entity.setName(name);
		entity.setKeyValue(keyValue);
		entity.setIsgroup(isgroup);
		//entity.setParentId(parentId);
		//entity.setDeleteFlag(deleteFlag);
		if(biz.findFunctionAuthorityByName(name) != null){
			model.addAttribute("msg","该名称已存在！");
			return "functionAuthority/add";
		}else{
			if(biz.add(entity) > 0){
				model.addAttribute("msg", "添加成功！");
				return "redirect:/functionAuthority/get";
			}else{
				model.addAttribute("msg","添加失败！");
				return "functionAuthority/add";
			}
		}
	}
	
	@RequestMapping("/export")
	public String exportData(){//导出数据到excel表
		return "functionAuthority/index";
	}
}